package com.cn.dao;

import java.util.ArrayList;
import com.cn.entity.Student;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-05-26
 * Time:14:38
 */
public class StudentDao {
    private ArrayList<Student> students;

    public StudentDao(ArrayList<Student> students) {
        this.students=students;
    }
    //增
    public void addStudent(String name,String id,int age,String hobby){
        boolean flg=false;
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i).getId().equals(id)){
                flg=true;
                System.out.println("已经有该学生的信息");
                break;
            }
        }
        if(flg==false){
            students.add(new Student(name, id, age, hobby));
        }
    }
    //删、
    public void delStudent(String id){
        boolean flg=false;
        int index=0;
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i).getId().equals(id)){
                flg=true;
                index=i;
                break;
            }
        }
        if(!flg){
            System.out.println("没有该学生的信息");
        }else {
            students.remove(index);
        }
    }
    //查
    public Student searchStudent(String id){
        boolean flg=false;
        int index=0;
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i).getId().equals(id)){
                flg=true;
                index=i;
                break;
            }
        }
        if(flg==true){
            return students.get(index);
        }else {
            System.out.println("没有该学生的信息");
            return null;
        }
    }
    //改
    public void changeStudent(Student student){
        Student ret=this.searchStudent(student.getId());
        if(ret==null){
            System.out.println("没有该学生的信息");
        }else{
            ret.setAge(student.getAge());
            ret.setName(student.getName());
            ret.setHobby(student.getHobby());
            ret.setId(student.getId());
        }
    }
    //删除所有学生
    public void clearStudent(){
        students.clear();
    }
    //查询所有学生
    public ArrayList<Student> searchAll(){
        return students;
    }
}
